package com.tevintao.marketingsystem.xposted;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
import android.text.TextUtils;

import com.tevintao.marketingsystem.aidl.ITaskCallback;
import com.tevintao.marketingsystem.aidl.ITaskLogic;
import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.AccountManager;
import com.tevintao.marketingsystem.services.MockServices;
import com.tevintao.marketingsystem.services.TaskLogicService;
import com.tevintao.marketingsystem.task.WxTaskProcess;
import com.tevintao.marketingsystem.task.WxUiViewConfig;
import com.tevintao.marketingsystem.tool.PackageTool;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;


public class WxTaskManger {
    public static final String TAG = WxTaskManger.class.getSimpleName();

    private static WxTaskManger ourInstance = null;

    public static WxTaskManger getInstance() {
        if (ourInstance == null) {
            synchronized (WxTaskProcess.class) {
                if (ourInstance == null) {
                    ourInstance = new WxTaskManger();
                }
            }
        }
        return ourInstance;
    }

    private WxTaskManger() {
    }

    public static final int TASK_SATE_SUCCESS = 0;
    public static final int TASK_STATE_TIME_OUT = 1; //任务超时
    public static final int TASK_STATE_ACCOUNT_BAN = 2; //封号
    public static final int TASK_SATE_EXTRA_ERROR = 3; //获取EXTRA数据失败
    public static final int TASK_SATE_NOT_FIND_VIEW = 4;//没有找到View
    public static final int TASK_SATE_PASSWORD_ERROR = 5;//帐号或者密码错误
    public static final int TASK_SATE_LOCAL_LOGIC_ERROR = 6;//TaskLogicService逻辑错误
    public static final int TASK_SATE_TASK_PAST = 7;//任务过期，或者已经完成过
    public static final int TASK_SATE_FIND_FRIEND_ERROR = 8;//找不到好友
    public static final int TASK_SATE_PHONE_VERIFICATION = 9;//手机验证
    public static final int TASK_SATE_LOCATION_ERROR = 10;//定位信息错误
    public static final int TASK_SATE_PROCESS_TASK = 11;//处理taskBean错误
    public static final int TASK_SATE_ACCOUNT_PAST = 12;//账号长期未用

    protected HandlerThread handlerThread;
    protected Handler mHandler;
    private Context mApplicationContext;

    public static final String ACTION_TASK_ERROR = "ACTION_TASK_ERROR";
    public static final String ACTION_HAVE_BACK_FINISH = "ACTION_HAVE_BACK_FINISH";
    public static final String KEY_TASK_ERROR_PACKAGE_NAME = "KEY_TASK_ERROR_PACKAGE_NAME";
    public static final String KEY_TASK_ERROR_STATE = "KEY_TASK_ERROR_STATE";
    public static final String KEY_TASK_ERROR_MESSAGE = "KEY_TASK_ERROR_MESSAGE";

    public static final String ACTION_TEST_SILK = "ACTION_TEST_SILK";
    public static final String ACTION_ADD_EXTRA_ICON = "ACTION_ADD_EXTRA_ICON";
    public static final String ACTION_ADD_EXTRA_VOICE_PATH = "ACTION_ADD_EXTRA_VOICE_PATH";

    public static final String KEY_TASK_ID = "KEY_TASK_ID";
    private int mTaskId = 0;
    public static final String KEY_TASK_MAX_TIME = "KEY_TASK_MAX_TIME";
    private int mMaxTime = 0;
    public static final String KEY_TASK_EXTRA = "KEY_TASK_EXTRA";
    private boolean need2Main = false;
    public static final String KEY_NEED_BACK_2_MAIN = "KEY_NEED_BACK_2_MAIN";
    private Map<String, Object> mExtraMap = new HashMap<>();

    private long mStartTaskTimeMillis = 0;

    private boolean mTaskRunning = false;

    public void processStart(XC_LoadPackage.LoadPackageParam loadPackageParam) {
        if ((AccountManager.WX_PACKAGE_NAME + ":push").equals(MockServices.getProcessName())) {
            LogManager.getInstance().logError(TAG + " processStart mStartConnectServices (AccountManager.WX_PACKAGE_NAME + \":push\") no nedd hook");
            return;
        }
        LogManager.getInstance().logInfo("processStart  myUid = " + Process.myUid() + " myPid = " + Process.myPid());
        handlerThread = new HandlerThread("HandlerThread myUid = " + Process.myUid() + " myPid = " + Process.myPid());
        handlerThread.start();
        mHandler = new Handler(handlerThread.getLooper());

        try {
            Class<?> ContextClass = XposedHelpers.findClass("android.content.ContextWrapper", loadPackageParam.classLoader);
            XposedHelpers.findAndHookMethod(ContextClass, "getApplicationContext", new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    super.afterHookedMethod(param);
                    if (mApplicationContext == null) {
                        mApplicationContext = (Context) param.getResult();
                        IntentFilter intentFilter = new IntentFilter();
                        intentFilter.addAction(ACTION_TASK_ERROR);
                        intentFilter.addAction(ACTION_HAVE_BACK_FINISH);
                        intentFilter.addAction(ACTION_TEST_SILK);
                        intentFilter.addAction(ACTION_ADD_EXTRA_ICON);
                        intentFilter.addAction(ACTION_ADD_EXTRA_VOICE_PATH);
                        intentFilter.addAction(HookSensor.ACTION_START_SHAKE);
                        intentFilter.addAction(HookWebView.ACTION_GET_LAST_HTML);
                        intentFilter.addAction(HookWebView.ACTION_GET_ELEMENT_RECT);
                        intentFilter.addAction(HookWebView.ACTION_DO_JAVA_SCRIPT);
                        mApplicationContext.registerReceiver(mTaskErrorReceiver, intentFilter);
                    }
//                    LogManager.getInstance().logInfo("processStart mApplicationContext = " + mApplicationContext);

                    mHandler.removeCallbacks(mCheckServiceConnected);
                    mHandler.postDelayed(mCheckServiceConnected, 100);
                }
            });
        } catch (Throwable t) {
            LogManager.getInstance().logErrorAndPrint("获取上下文出错", t);
        }
    }

    private BroadcastReceiver mTaskErrorReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (TextUtils.isEmpty(action)) {
                return;
            }
            if (ACTION_TASK_ERROR.equals(intent.getAction())) {
                Bundle extras = intent.getExtras();
                if (null == extras) {
                    LogManager.getInstance().logError("mTaskErrorReceiver null == extras");
                    return;
                }
                int state = extras.getInt(KEY_TASK_ERROR_STATE);
                String packageName = extras.getString(KEY_TASK_ERROR_PACKAGE_NAME);
                String message = extras.getString(KEY_TASK_ERROR_MESSAGE);
                String currentProcessName = PackageTool.getProcessName(context);
                LogManager.getInstance().logError("mTaskErrorReceiver ACTION_TASK_ERROR state = " + state + " message = " + message + " 消息来源进程名称 = " + packageName + " 当前进程名称 = " + currentProcessName);
                if (AccountManager.WX_PACKAGE_NAME.equals(currentProcessName)) {
                    taskError(state, message);
                } else {
                    LogManager.getInstance().logError("mTaskErrorReceiver Process name != message package name");
                }
            } else if (HookSensor.ACTION_START_SHAKE.equals(action)) {
                HookSensor.getInstance().startShake();
            } else if (HookWebView.ACTION_GET_LAST_HTML.equals(action)) {
                HookWebView.getInstance().loadHtml();
                mExtraMap.put(HookWebView.KEY_LAST_HTML, HookWebView.mLastHtml);
            } else if (HookWebView.ACTION_GET_ELEMENT_RECT.equals(action)) {
                mExtraMap.put(HookWebView.KEY_GET_ELEMENT_RECT, HookWebView.mLastRect);
            } else if (HookWebView.ACTION_DO_JAVA_SCRIPT.equals(action)) {
                Bundle extras = intent.getExtras();
                if (null == extras) {
                    LogManager.getInstance().logError("mTaskErrorReceiver null == extras");
                    return;
                }
                String script = extras.getString(HookWebView.KEY_DO_JAVA_SCRIPT);
                LogManager.getInstance().logInfo("WxTaskManager script " + script);
                if (TextUtils.isEmpty(script)) {
                    return;
                }
                HookWebView.getInstance().doScript(script);
            } else if (ACTION_HAVE_BACK_FINISH.equals(action)) {
                mExtraMap.put(KEY_NEED_BACK_2_MAIN, false);
            } else if (ACTION_ADD_EXTRA_ICON.equals(action)) {
                Bundle extras = intent.getExtras();
                LogManager.getInstance().logError("ACTION_ADD_EXTRA_ICON extras = " + extras);
                if (null != extras && extras.containsKey(WxUiViewConfig.KEY_SEND_PIC_MESSAGE)) {
                    mExtraMap.put(WxUiViewConfig.KEY_SEND_PIC_MESSAGE, extras.getString(WxUiViewConfig.KEY_SEND_PIC_MESSAGE));
                    LogManager.getInstance().logInfo("KEY_SEND_PIC_MESSAGE icon = " + extras.getString(WxUiViewConfig.KEY_SEND_PIC_MESSAGE) + " getExtra(WxUiViewConfig.KEY_SEND_PIC_MESSAGE) = " + getExtra(WxUiViewConfig.KEY_SEND_PIC_MESSAGE));
                }
            } else if (ACTION_ADD_EXTRA_VOICE_PATH.equals(action)) {
                Bundle extras = intent.getExtras();
                LogManager.getInstance().logError("ACTION_ADD_EXTRA_VOICE_PATH extras = " + extras);

                if (null != extras && extras.containsKey(HookAudioRecord.KEY_AUDIO_PATH)) {
                    mExtraMap.put(HookAudioRecord.KEY_AUDIO_PATH, extras.getString(HookAudioRecord.KEY_AUDIO_PATH));
                    LogManager.getInstance().logInfo("KEY_AUDIO_PATH icon = " + extras.getString(HookAudioRecord.KEY_AUDIO_PATH) + " getExtra(HookAudioRecord.KEY_AUDIO_PATH) = " + getExtra(HookAudioRecord.KEY_AUDIO_PATH));
                }
            }else if (ACTION_TEST_SILK.equals(action)) {
                if ("com.tencent.mm".equals(MockServices.getProcessName())) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            getWxCoder();
                        }
                    }).start();
                }
            }
        }
    };

    private Runnable mCheckServiceConnected = new Runnable() {
        @Override
        public void run() {
            boolean hasConnected = false;
            if (null != mService) {
                try {
                    hasConnected = mService.serviceIsAlive();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
            if (!hasConnected) { //没有连接
                LogManager.getInstance().logError("!hasConnected 服务没有连接");
                mHandler.removeCallbacks(mStartConnectServices);
                mHandler.postDelayed(mStartConnectServices, 100);
            }
            mHandler.postDelayed(mCheckServiceConnected, 5000);
        }
    };


    private Runnable mStartConnectServices = new Runnable() {
        @Override
        public void run() {
            LogManager.getInstance().logInfo("mStartConnectServices  myUid = " + Process.myUid() + " myPid = " + Process.myPid() + " processName" + MockServices.getProcessName());
            if (null != mApplicationContext) {
                Intent mIntent = new Intent();
                mIntent.setAction(TaskLogicService.ACTION_START_TASK_LOGIC_SERVICE);//你定义的service的action
                mIntent.setPackage(HookDeviceModule.SELF_PACKAGE_NAME);//这里你需要设置你应用的包名
                mApplicationContext.bindService(mIntent, mConnection, Context.BIND_AUTO_CREATE);
            } else {
                LogManager.getInstance().logError("mStartConnectServices null == mApplicationContext");
            }
        }
    };

    private ITaskLogic mService;
    private ServiceConnection mConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName className, IBinder service) {
//            LogManager.getInstance().logInfo("onServiceConnected  className = " + className);
            mService = ITaskLogic.Stub.asInterface(service);
            String processName = MockServices.getProcessName();
            try {
                boolean success = mService.registerSyncCallback(processName, taskCallback);
                LogManager.getInstance().logInfo("mStartConnectServices success = " + success + " ProcessName " + MockServices.getProcessName());
            } catch (RemoteException e) {
                LogManager.getInstance().logErrorAndPrint("ServiceConnection RemoteException", e);
            }
        }

        public void onServiceDisconnected(ComponentName className) {
            LogManager.getInstance().logInfo("mStartConnectServices onServiceDisconnected className = " + className);
            mService = null;
        }
    };

    private ITaskCallback taskCallback = new ITaskCallback.Stub() {
        @Override
        public boolean startTask(Bundle bundle) throws RemoteException {
            return startViewTask(bundle);
        }

        @Override
        public boolean stopTask() throws RemoteException {
            clearTaskInfo();
            mTaskRunning = false;
            return true;
        }

        @Override
        public boolean clientAlive() throws RemoteException {
            return true;
        }

        @Override
        public boolean processRun() throws RemoteException {
            return mTaskRunning;
        }
    };

    private void clearTaskInfo() {
        mTaskId = 0;
        mMaxTime = 0;
        mExtraMap.clear();
        mStartTaskTimeMillis = 0;
    }

    private boolean startViewTask(Bundle bundle) {
        if (null == bundle) {
            return false;
        }
        int id = bundle.getInt(KEY_TASK_ID);
        if (id > 0) {
            mTaskId = id;
        } else {
            LogManager.getInstance().logError("startViewTask id < 0 ");
            return false;
        }
        mMaxTime = bundle.getInt(KEY_TASK_MAX_TIME);
        need2Main = bundle.getBoolean(KEY_NEED_BACK_2_MAIN);
        mExtraMap.put(KEY_NEED_BACK_2_MAIN, need2Main);
        String extraJson = bundle.getString(KEY_TASK_EXTRA);
        LogManager.getInstance().logInfo("startTask id = " + id + " extraJson = " + extraJson + " need2Main = " + need2Main);
        if (null == extraJson) {
            return false;
        }
        try {
            JSONObject jo = new JSONObject(extraJson);
            Iterator<String> keys = jo.keys();
            while (keys.hasNext()) {
                String key = keys.next();
                mExtraMap.put(key, jo.get(key));
            }
        } catch (JSONException e) {
            e.printStackTrace();
            LogManager.getInstance().logErrorAndPrint("startViewTask JSONException extraJson = " + extraJson, e);
            return false;
        }
        mStartTaskTimeMillis = System.currentTimeMillis();
        mTaskRunning = true;
        WxLocationManager.getInstance().clearCache();//清理掉位置信息，保证每次任务都是最新的位置信息
        mHandler.postDelayed(mProcessViewRunnable, 5000);
        return true;
    }


    private Runnable mProcessViewRunnable = new Runnable() {
        @Override
        public void run() {
            mHandler.removeCallbacks(mProcessViewRunnable);
            if (mTaskRunning) {
                if (System.currentTimeMillis() - mStartTaskTimeMillis > mMaxTime * 60 * 1000) {
                    LogManager.getInstance().logInfo(" System.currentTimeMillis() = " + System.currentTimeMillis() + " mStartTaskTimeMillis = " + mStartTaskTimeMillis + " mMaxTime = " + mMaxTime);
                    if (AccountManager.WX_PACKAGE_NAME.equals(MockServices.getProcessName())) {
                        LogManager.getInstance().logError("任务超时");
                        taskError(TASK_STATE_TIME_OUT, "任务超时 id = " + mTaskId);
                    } else {
                        LogManager.getInstance().logInfo("任务超时但不再主进程中");
                    }

                    return;
                }
                WxTaskProcess.getInstance().processView();
            }
            mHandler.postDelayed(mProcessViewRunnable, 2000);
        }
    };

    public int getTaskId() {
        return mTaskId;
    }

    public boolean isNeed2Main() {
        return need2Main;
    }

    public void setNeed2Main(boolean need) {
        need2Main = need;
    }

    public Map<String, Object> getExtraMap() {
        return mExtraMap;
    }

    public Object getExtra(String key) {
        return mExtraMap.get(key);
    }

    public boolean taskError(int state, String message) {
        if (null != mService) {
            try {
                boolean success = mService.taskFinish(state, message);
                LogManager.getInstance().logError("taskError success = " + success + " ProcessName " + MockServices.getProcessName());
                if (success) {
                    mTaskRunning = false;
                }
                return success;
            } catch (RemoteException e) {
                e.printStackTrace();
                LogManager.getInstance().logErrorAndPrint("taskError RemoteException ", e);
            }
        } else {
            LogManager.getInstance().logError("taskError mService = null " + mService + " ProcessName " + MockServices.getProcessName() + "  myUid = " + Process.myUid() + " myPid = " + Process.myPid() + " myTid = " + Process.myTid());
        }
        return false;
    }

    private void getWxCoder() {
        int bufferSizeInBytes = 20480;
        Class wxMediaRecorderClass = null;
        try {
            wxMediaRecorderClass = mApplicationContext.getClassLoader().loadClass("com.tencent.mm.modelvoice.MediaRecorder");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("getTaskClass ClassNotFoundException name = " + "com.tencent.mm.modelvoice.MediaRecorder");
        }

        LogManager.getInstance().logInfo("wxMediaRecorderClass  =  " + wxMediaRecorderClass);
        if (null != wxMediaRecorderClass) {
            for (Method method : wxMediaRecorderClass.getDeclaredMethods()) {
                LogManager.getInstance().logInfo("method   =   " + method);
            }
        } else {
            return;
        }

        Object bzqObj = null;
        try {
            Field bZQ = wxMediaRecorderClass.getDeclaredField("bZQ");
            bZQ.setAccessible(true);
            bzqObj = bZQ.get(null);
            LogManager.getInstance().logInfo("bzqObj = " + bzqObj);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("NoSuchFieldException  =  ");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("IllegalAccessException  =  ");
        }

        if (null == bzqObj) {
            LogManager.getInstance().logError("null == bzqObj");
            return;
        }

        File f = new File("/mnt/sdcard/ms/out.amr");
        if (!f.exists()) {
            return;
        }
        int fileLength = 0;
        byte[] bytes = new byte[(int) f.length()];
        try {
            FileInputStream fileInputStream = new FileInputStream(f);
            fileLength = fileInputStream.read(bytes);
            LogManager.getInstance().logInfo("read fileLength = " + fileLength);
            fileInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("FileNotFoundException   fileInputStream =  ");
        } catch (IOException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("IOException   fileInputStream =  ");
        }

        int rate = 0;
        try {
            Method SilkGetEncSampleRate = wxMediaRecorderClass.getDeclaredMethod("SilkGetEncSampleRate", byte[].class);
            Object result = SilkGetEncSampleRate.invoke(null, bytes);
            LogManager.getInstance().logInfo("SilkGetEncSampleRate result = " + result);
            if (result instanceof Integer) {
                rate = (int) result;
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("NoSuchMethodException   wxMediaRecorderClass =  ");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("IllegalAccessException   wxMediaRecorderClass =  ");
        } catch (InvocationTargetException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("InvocationTargetException   wxMediaRecorderClass =  ");
        }

        LogManager.getInstance().logInfo("rate = " + rate);
        try {
            Method SilkDecUnInit = wxMediaRecorderClass.getDeclaredMethod("SilkDecUnInit");
            Object result = SilkDecUnInit.invoke(bzqObj);
            LogManager.getInstance().logInfo("SilkDecUnInit result = " + result);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("NoSuchMethodException   SilkDecUnInit =  ");
        } catch (InvocationTargetException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("InvocationTargetException   SilkDecUnInit =  ");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("IllegalAccessException   SilkDecUnInit =  ");
        }

        try {
            Method SilkDecInit = wxMediaRecorderClass.getDeclaredMethod("SilkDecInit", int.class, byte[].class, int.class);
            Object result = SilkDecInit.invoke(bzqObj, rate, bytes, fileLength);
            LogManager.getInstance().logInfo("SilkDecInit result = " + result + " " + MockServices.getProcessName());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("NoSuchMethodException   SilkDecInit =  ");
        } catch (InvocationTargetException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("InvocationTargetException   SilkDecInit =  ");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("IllegalAccessException   SilkDecInit =  ");
        }
        FileOutputStream fileOutputStream = null;
        try {
            File outFile = new File("/mnt/sdcard/ms/out.pcm");
            if (outFile.exists()) {
                outFile.delete();
            }
            try {
                outFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
                LogManager.getInstance().logError("创建文件失败");
                return;
            }
            fileOutputStream = new FileOutputStream(outFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (null == fileOutputStream) {
            LogManager.getInstance().logError("创建输出流失败");
        }
        int bufferSize = AudioRecord.getMinBufferSize(rate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
        LogManager.getInstance().logInfo("bufferSize = " + bufferSize);
        try {
            Method SilkDoDec = wxMediaRecorderClass.getDeclaredMethod("SilkDoDec", byte[].class, short.class);
            AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, rate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSizeInBytes, AudioTrack.MODE_STREAM);
            audioTrack.play();
            byte[] playBuffer = new byte[bufferSize * 2];
            short playLen = (short) (bufferSizeInBytes / 64);
            int resultCode = 1;
            while (resultCode != 0) {
                Object decObj = SilkDoDec.invoke(null, playBuffer, playLen);
                if (decObj instanceof Integer) {
                    resultCode = (int) decObj;
                }
                LogManager.getInstance().logInfo("resultCode = " + resultCode);
                audioTrack.write(playBuffer, 0, playLen * 2);

                fileOutputStream.write(playBuffer, 0, playLen * 2);
            }
            audioTrack.stop();
            fileOutputStream.close();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("NoSuchMethodException   SilkDoDec =  " + e);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("InvocationTargetException   SilkDoDec =  " + e);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("IllegalAccessException   SilkDoDec =  " + e);
        } catch (Exception e) {
            e.printStackTrace();
            LogManager.getInstance().logError("Exception   SilkDoDec =  " + e);
        }

    }
}
